package com.kelab.dify.config;

/**
 * Dify客户端配置类
 * 
 * @author kelab
 * @version 1.0
 */
public class DifyConfig {
    
    // 默认配置值
    private static final String DEFAULT_BASE_URL = "https://api.dify.ai/v1";
    private static final long DEFAULT_CONNECT_TIMEOUT = 30000; // 30秒
    private static final long DEFAULT_READ_TIMEOUT = 60000;    // 60秒
    private static final long DEFAULT_WRITE_TIMEOUT = 60000;   // 60秒
    
    private String baseUrl;
    private String apiKey;
    private long connectTimeout;
    private long readTimeout;
    private long writeTimeout;
    private boolean enableLogging;
    
    /**
     * 私有构造函数，使用Builder模式
     */
    private DifyConfig(Builder builder) {
        this.baseUrl = builder.baseUrl;
        this.apiKey = builder.apiKey;
        this.connectTimeout = builder.connectTimeout;
        this.readTimeout = builder.readTimeout;
        this.writeTimeout = builder.writeTimeout;
        this.enableLogging = builder.enableLogging;
    }
    
    /**
     * 创建Builder实例
     * 
     * @param apiKey API密钥
     * @return Builder实例
     */
    public static Builder builder(String apiKey) {
        return new Builder(apiKey);
    }
    
    /**
     * 创建默认配置
     * 
     * @param apiKey API密钥
     * @return DifyConfig实例
     */
    public static DifyConfig defaultConfig(String apiKey) {
        return builder(apiKey).build();
    }
    
    // Getter方法
    public String getBaseUrl() {
        return baseUrl;
    }
    
    public String getApiKey() {
        return apiKey;
    }
    
    public long getConnectTimeout() {
        return connectTimeout;
    }
    
    public long getReadTimeout() {
        return readTimeout;
    }
    
    public long getWriteTimeout() {
        return writeTimeout;
    }
    
    public boolean isEnableLogging() {
        return enableLogging;
    }
    
    /**
     * 获取完整的API URL
     * 
     * @param endpoint 端点路径
     * @return 完整的URL
     */
    public String getFullUrl(String endpoint) {
        String url = baseUrl;
        if (!url.endsWith("/")) {
            url += "/";
        }
        if (endpoint.startsWith("/")) {
            endpoint = endpoint.substring(1);
        }
        return url + endpoint;
    }
    
    /**
     * 获取Authorization头部值
     * 
     * @return Bearer token格式的授权头
     */
    public String getAuthorizationHeader() {
        return "Bearer " + apiKey;
    }
    
    /**
     * Builder类用于构建DifyConfig实例
     */
    public static class Builder {
        private String baseUrl = DEFAULT_BASE_URL;
        private String apiKey;
        private long connectTimeout = DEFAULT_CONNECT_TIMEOUT;
        private long readTimeout = DEFAULT_READ_TIMEOUT;
        private long writeTimeout = DEFAULT_WRITE_TIMEOUT;
        private boolean enableLogging = false;
        
        private Builder(String apiKey) {
            if (apiKey == null || apiKey.trim().isEmpty()) {
                throw new IllegalArgumentException("API key cannot be null or empty");
            }
            this.apiKey = apiKey.trim();
        }
        
        /**
         * 设置基础URL
         * 
         * @param baseUrl 基础URL
         * @return Builder实例
         */
        public Builder baseUrl(String baseUrl) {
            if (baseUrl != null && !baseUrl.trim().isEmpty()) {
                this.baseUrl = baseUrl.trim();
                // 移除末尾的斜杠
                if (this.baseUrl.endsWith("/")) {
                    this.baseUrl = this.baseUrl.substring(0, this.baseUrl.length() - 1);
                }
            }
            return this;
        }
        
        /**
         * 设置连接超时时间
         * 
         * @param connectTimeout 连接超时时间（毫秒）
         * @return Builder实例
         */
        public Builder connectTimeout(long connectTimeout) {
            if (connectTimeout > 0) {
                this.connectTimeout = connectTimeout;
            }
            return this;
        }
        
        /**
         * 设置读取超时时间
         * 
         * @param readTimeout 读取超时时间（毫秒）
         * @return Builder实例
         */
        public Builder readTimeout(long readTimeout) {
            if (readTimeout > 0) {
                this.readTimeout = readTimeout;
            }
            return this;
        }
        
        /**
         * 设置写入超时时间
         * 
         * @param writeTimeout 写入超时时间（毫秒）
         * @return Builder实例
         */
        public Builder writeTimeout(long writeTimeout) {
            if (writeTimeout > 0) {
                this.writeTimeout = writeTimeout;
            }
            return this;
        }
        
        /**
         * 启用日志记录
         * 
         * @param enableLogging 是否启用日志
         * @return Builder实例
         */
        public Builder enableLogging(boolean enableLogging) {
            this.enableLogging = enableLogging;
            return this;
        }
        
        /**
         * 构建DifyConfig实例
         * 
         * @return DifyConfig实例
         */
        public DifyConfig build() {
            return new DifyConfig(this);
        }
    }
    
    @Override
    public String toString() {
        return "DifyConfig{" +
                "baseUrl='" + baseUrl + '\'' +
                ", apiKey='***'" + // 隐藏API密钥
                ", connectTimeout=" + connectTimeout +
                ", readTimeout=" + readTimeout +
                ", writeTimeout=" + writeTimeout +
                ", enableLogging=" + enableLogging +
                '}';
    }
}